#!/usr/bin/python2.6
import sys

basedir = '.'

if len(sys.argv) < 2 :
	print >> sys.stderr, '-m merge or -a append'
	sys.exit(1)
flag = sys.argv[1]
if flag != '-m' and flag != '-a':
	print >> sys.stderr, '-m merge or -a append'
	sys.exit(2)

print >> sys.stderr, 'setup...',

TAG_POS = 1<<24
TAG_GRAM = 2<<24
TAG_NR = 3<<24
TAG_NS = 4<<24
TAG_TR = 5<<24
TAG_UNKNOWN = 6<<24

i = 0
preword = ''
freq = 0
posn = 0
tagk = TAG_POS
for line in sys.stdin:
	i += 1
	arr = line.split()
	if len(arr) != 3 :
		print >> sys.stderr, 'error line[', i, ']: ', line
		continue
	word = arr[0]
	if flag == '-m' :
		if word != preword :
			if len(preword) > 0 :
				print >> sys.stdout, preword, '\t', posn, '\t', freq
			freq = 0;
			preword = word
		freq += int(arr[2])
		posn = 1 + tagk
	else :
		freq = int(arr[2])
		posn = arr[1]
		if word != preword :
			if len(preword) > 0 :
				print >> sys.stdout, '\n',
			print >> sys.stdout, word, '\t', posn, '\t', freq,
			preword = word
		else :
			print >> sys.stdout, '\t', posn, '\t', freq,

if flag == '-m' :
	if len(preword) > 0 :
		print >> sys.stdout, preword, '\t', posn, '\t', freq
else :
	print >> sys.stdout, '\n',

print >> sys.stderr, '\r--setup ', flag, '--'
